package com.uinnova.product.eam.workable.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Map;

@Slf4j
@Component("reSubmitToRejectMuilityParseUserListener")
public class ReSubmitToRejectMuilityParseUserListener implements ExecutionListener {

    @Override
    public void notify(DelegateExecution execution) {
        Map<String, Object> variables = execution.getVariables();
        Object childVariable = variables.get("childVariable");
        if (childVariable != null) {
            log.info("子流程变量：{}", childVariable);
            if (childVariable instanceof String) {
                String jsonString = (String) childVariable;
                try {
                    JSONObject jsonObject = JSON.parseObject(jsonString);
                    JSONArray rejectUsers = jsonObject.getJSONArray("rejectUsers");
                    variables.put("assigneeList", rejectUsers);
                    execution.setVariables(variables);
                } catch (Exception e) {
                    log.error("解析子流程变量异常", e);
                }
            }
        }
    }
}
